﻿package base {
	
	
	import flash.display.MovieClip;
	import flash.events.Event;
	import core.events.CmdEvent;
	import core.net.socket.CmdHandler;
	import core.net.socket.SocketClient;
	import core.net.socket.SocketCmd;
	import core.log.Logger;
	import flashx.textLayout.formats.Float;
	
	public class BaseMc extends MovieClip {
		
		private var cmdHandler:CmdHandler = CmdHandler.getInstance();
		private var logger:Logger = Logger.getInstance();
		public static var ConnectFailed:String = "CONNECTFAILED";
		public static var ConnectSuccess:String = "CONNECTSUCCESS";
		public function BaseMc() {
			this.addEventListener(Event.REMOVED_FROM_STAGE,onRemoveFromStage);
			this.addEventListener(Event.ADDED_TO_STAGE ,onAddedToStage);
		}
		
		
		private function onRemoveFromStage(e:Event):void {
			this.addEventListener(Event.ADDED_TO_STAGE ,onAddedToStage);
			this.cmdHandler.removeEventListener(CmdEvent.RESPONSE,onCommandResponse);
			this.onRemove();
		}
		
		private function onAddedToStage(e:Event):void {
			this.removeEventListener(Event.ADDED_TO_STAGE ,onAddedToStage);//直接移除监听，防止多次加载问题
			
			this.cmdHandler.addEventListener(CmdEvent.RESPONSE,onCommandResponse);
			this.onInit();
		}
		
		
		protected function getLogger():Logger{
			return this.logger;
		}
		
		protected function debug(str:String):void {
			this.getLogger().debug(str);
		}
		
		protected function error(str:String):void {
			this.getLogger().error(str);
		}
		
		
		public function remove():void  {
			if(this.parent != null){
				this.parent.removeChild(this);
			}
			
		}
		
		public function sendCmd(cmd:SocketCmd):void {
			this.cmdHandler.send(cmd);
		}
		
		public function centerOfStage():void{
			this.x = (this.stage.stageWidth - this.width) / 2;
			this.y = (this.stage.stageHeight - this.height) / 2;
		}
		
		public function centerOfParent():void {
			this.x = (this.parent.width - this.width) / 2;
			this.y = (this.parent.height - this.height) / 2;
		}
		
		public function hide():void {
			this.visible = false;
		}
		
		public function show():void {
			this.visible = true;
		}
		
		public function scale(x:Number,y:Number):void {
			this.scaleX = 1;
			this.scaleY = 1;
			this.scaleX *= x;
			this.scaleY *= y;
		}
		
		//当影片放到场景上后的初始化方法
		protected function onInit():void {
			
		}
		
		//当影片从场景上移除的时候周期方法 
		protected function onRemove():void {
			
		}
		
		//监听SocketServer返回的内容
		protected function onCommandResponse(e:CmdEvent):void {
			
		}
		
		
	}
	
}
